package org.example.common.util;

import lombok.extern.slf4j.Slf4j;
import org.example.common.config.BaseErrorException;
import org.example.enums.BaseErrorEnums;

@Slf4j
public class ThrowBaseErrorUtils {

    /**
     * 抛出异常
     *
     * @param baseErrorEnums
     */
    public static void throwError(BaseErrorEnums baseErrorEnums) {
        RuntimeException runtimeException = new RuntimeException();
        StackTraceElement[] stackTrace = runtimeException.getStackTrace();
        StackTraceElement stackTraceElement = null;
        if (stackTrace.length > 1) {
            stackTraceElement = stackTrace[stackTrace.length - 2];
        } else {
            stackTraceElement = stackTrace[stackTrace.length - 1];
        }
        try {
            log.info("类:{},linenumber：{},error:{}", stackTraceElement.getClassName(),stackTraceElement.getLineNumber(), baseErrorEnums.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new BaseErrorException(baseErrorEnums);
    }
}
